{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dc070f55-5f00-4331-894e-3b1827a989a1",
   "metadata": {},
   "source": [
    "Chapter 09\n",
    "# 定义、使用“矩形”类\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de40e442-e988-4526-b325-045041f627b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个名为Rectangle的类\n",
    "class Rectangle:\n",
    "    # 创建Rectangle对象时执行一些初始化工作\n",
    "    def __init__(self, width, height):\n",
    "        # 设置实例变量self.width来存储传入的宽度参数\n",
    "        self.width = width\n",
    "        # 设置实例变量self.height来存储传入的高度参数\n",
    "        self.height = height\n",
    "    \n",
    "    # 定义一个名为circumference的方法，用于计算矩形的周长\n",
    "    def circumference(self):\n",
    "        # 返回矩形的周长，计算公式为2*(宽度 + 高度)\n",
    "        return 2*(self.width + self.height)\n",
    "    \n",
    "    # 定义一个名为area的方法，用于计算矩形的面积\n",
    "    def area(self):\n",
    "        # 返回矩形的面积，计算公式为宽度 * 高度\n",
    "        return self.width * self.height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "729bf2e9-1376-4300-b4cb-ba64f3df7c52",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用Rectangle类\n",
    "# 定义矩形，宽5，高10\n",
    "rect_width_5_height_10 = Rectangle(5, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6fc71f8-b976-4840-a667-8a212662885c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩形周长\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "print('矩形周长')\n",
    "print(rect_width_5_height_10.circumference())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cea7fb93-971b-4186-ba6a-45a120142d97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩形面积\n",
      "50\n"
     ]
    }
   ],
   "source": [
    "print('矩形面积')\n",
    "print(rect_width_5_height_10.area())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3985dd19-436e-431f-9987-fb1f5aa06093",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Repo: https://github.com/Visualize-ML\n",
    "# Book 1 Python for Beginners  |  From Basic Arithmetic to Machine Learning\n",
    "# Published and copyrighted by Tsinghua University Press\n",
    "# Beijing, China, 2023"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
